﻿using System.Linq.Expressions;
using ApiLib.Models;
using SqlSugar;

namespace ApiLib.Utils;

/// <summary>
/// EFCore工具
/// </summary>
public static class EfCoreUtil
{
    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="source">源</param>
    /// <param name="pageIndex">页码</param>
    /// <param name="pageSize">每页数据量</param>
    /// <param name="totalCount">总数</param>
    /// <typeparam name="T">实体类</typeparam>
    /// <returns></returns>
    public static List<T> PageList<T>(this IQueryable<T> source, int pageIndex, int pageSize, out int totalCount)
    {
        totalCount = source.Count();

        /*if (pageIndex < 1)
        {
            pageIndex = 1;
        }

        var pageCount = (int)Math.Ceiling(totalCount * 1.0 / pageSize);
        if (pageIndex > pageCount)
        {
            pageIndex = pageCount;
        }*/

        return source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
    }

    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="source">源</param>
    /// <param name="pageParam">分页信息</param>
    /// <typeparam name="T">实体类</typeparam>
    /// <returns></returns>
    public static List<T> PageList<T>(this IQueryable<T> source, PageModel pageParam)
    {
        var list = source.PageList(pageParam.PageIndex, pageParam.PageSize, out var totalCount);
        pageParam.TotalCount = totalCount;
        return list;
    }

    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="source">源</param>
    /// <param name="pageIndex">页码</param>
    /// <param name="pageSize">每页数据量</param>
    /// <typeparam name="T">实体类</typeparam>
    /// <returns></returns>
    public static ListResponse<T> SimplePageList<T>(this IQueryable<T> source, int pageIndex, int pageSize)
    {
        var list = source.PageList(pageIndex, pageSize, out var totalCount);
        return new ListResponse<T> { List = list, TotalCount = totalCount };
    }

    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="source">源</param>
    /// <param name="pageParam">分页</param>
    /// <typeparam name="T">实体类</typeparam>
    /// <returns></returns>
    public static ListResponse<T> SimplePageList<T>(this IQueryable<T> source, PageParam pageParam)
    {
        var list = source.PageList(pageParam.PageIndex, pageParam.PageSize, out var totalCount);
        return new ListResponse<T> { List = list, TotalCount = totalCount };
    }

    /// <summary>
    /// 条件不为空查询
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="baseQuery"></param>
    /// <param name="val">字符串</param>
    /// <param name="where"></param>
    /// <returns></returns>
    public static IQueryable<T> WhereIfNotEmpty<T>(this IQueryable<T> baseQuery, string? val,
        Expression<Func<T, bool>> where)
    {
        return !string.IsNullOrEmpty(val) ? baseQuery.Where(where) : baseQuery;
    }

    /// <summary>
    /// 条件查询
    /// </summary>
    /// <param name="baseQuery"></param>
    /// <param name="flag">条件</param>
    /// <param name="where"></param>
    /// <returns></returns>
    public static IQueryable<T> WhereIf<T>(this IQueryable<T> baseQuery, bool flag, Expression<Func<T, bool>> where)
    {
        return flag ? baseQuery.Where(where) : baseQuery;
    }
}